package org.dairy.farms.dao.hibernate;

import org.dairy.farms.dao.ExpenseDAO;
import org.dairy.farms.dao.RevenueDAO;
import org.dairy.farms.dao.generic.AbstractDAOImpl;
import org.dairy.farms.model.*;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gduggira
 * Date: Jul 13, 2011
 * Time: 3:52:46 PM
 */
@Repository("revenueDao")
public class RevenueDAOImpl extends AbstractDAOImpl<Revenue, Long> implements RevenueDAO {

    @Override
    public List<Revenue> findByLivestockId(Long livestockId, int page, int start, int limit) {
        Criteria criteria = basePersistence.getSession().createCriteria(LivestockRevenue.class);
        criteria.createCriteria("livestock").add(Restrictions.eq("id", livestockId)).setFirstResult(start).setMaxResults(limit);
        return criteria.list();
    }

    @Override
    public List<Revenue> findByLivestockId(Long livestockId) {
        Criteria criteria = basePersistence.getSession().createCriteria(Revenue.class);
        criteria.createCriteria("revenueType", "rt");
        criteria.add(Restrictions.eq("rt.revenueTypeOf", RevenueType.LIVESTOCK_REVENUE));
        /*Criterion lactationRevenue = Restrictions.eq("class", LactationRevenue.class);
        Criterion livestockRevenue = Restrictions.eq("class", LivestockRevenue.class);

        Disjunction disjunction = Restrictions.disjunction();
        disjunction.add(lactationRevenue);
        disjunction.add(livestockRevenue);*/

        return criteria.list();
    }
}
